package cn.edu.ujn.lab3.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mysql.jdbc.StringUtils;

import cn.edu.ujn.lab3.dao.User;
import cn.edu.ujn.lab3.dao.UserMapper;
import cn.edu.ujn.lab3.utils.Page;
@Service("userService")
@Transactional
public class UserServiceImpl implements IUserService {

	@Autowired
	private UserMapper userdao;
	
	 
	
	
	@Override
	public int deleteByPrimaryKey(Integer userId) {
		// TODO Auto-generated method stub
		int key = userdao.deleteByPrimaryKey(userId);
		return key;
	}

	@Override
	public int insert(User row) {
		// TODO Auto-generated method stub
		int insert = userdao.insert(row);
		return insert;
	}

	@Override
	public int insertSelective(User row) {
		// TODO Auto-generated method stub
		int selective = userdao.insertSelective(row);
		return selective;
	}

	@Override
	public User selectByPrimaryKey(Integer userId) {
		// TODO Auto-generated method stub
		User user = userdao.selectByPrimaryKey(userId);
		return user;
	}

	@Override
	public int updateByPrimaryKeySelective(User row) {
		// TODO Auto-generated method stub
		int update = userdao.updateByPrimaryKeySelective(row);
		return update;
	}

	@Override
	public int updateByPrimaryKey(User row) {
		// TODO Auto-generated method stub
		int update = userdao.updateByPrimaryKey(row);
		return update;
	}

	@Override
	public User findUserByCodeAndPassword(String usercode, String password) {
		// TODO Auto-generated method stub
		return userdao.findUserByCodeAndPassword(usercode, password);
	}

	@Override
	public List<User> selectUserList(User user) {
		// TODO Auto-generated method stub
		return userdao.selectUserList(user);
	}

	@Override
	public int selectUserCount() {
		// TODO Auto-generated method stub
		return userdao.selectUserCount();
	}
	
	//用户列表
	@Override
	public Page<User> findUserList(Integer page, Integer rows, Integer user_id, String user_code,String user_name,String user_password,Integer user_state) {
		// TODO Auto-generated method stub
		//创建用户对象
		User user=new User();
		user.setUser_id(user_id);
		user.setUser_state(user_state);
		if(!StringUtils.isNullOrEmpty(user_code)) {
			user.setUser_code(user_code);
		}
		if(!StringUtils.isNullOrEmpty(user_name)) {
			user.setUser_name(user_name);	
		}
		if(!StringUtils.isNullOrEmpty(user_password)) {
			user.setUser_password(user_password);
		}
		//当前页
		user.setStart((page-1) * rows);
		//每页数
		user.setRows(rows);
		//查询用户列表
		List<User> users=this.userdao.selectUserList(user);
		//查询用户列表总记录数
		Integer count=this.userdao.selectUserCount();
		//创建Page返回对象
		Page<User> result=new Page<User>();
		result.setPage(page);
		result.setRows(users);
		result.setSize(rows);
		result.setTotal(count);
		return result;
	}

	@Override
	public int updateByUserCodeSelective(User row) {
		// TODO Auto-generated method stub
		int key = userdao.updateByUserCodeSelective(row);
		return key;
	}

	@Override
	public User selectByUserCode(String userCode) {
		// TODO Auto-generated method stub
		User user = userdao.selectByUserCode(userCode);
		return user;
	}



}
